﻿#region MIT
/*
Engine Of Evermore (http://code.google.com/p/engineofevermore/)
Copyright (c) 2008 Michael Woerister

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace EngineOfEvermore.Common
{
    public static class Util
    {
        public const float STANDARD_EPSILON = 0.001f;
        public const float NEGATIVE_STANDARD_EPSILON = -STANDARD_EPSILON;
        public const float FLOAT_PI = (float) Math.PI;

        public static bool EpsilonEquals( this float a, float b, float epsilon )
        {
            Debug.Assert( epsilon > 0.0f );

            float diff = a - b;
            return ( diff <= epsilon ) && ( diff >= -epsilon );
        }

        public static bool EpsilonEquals( this float a, float b )
        {
            float diff = a - b;
            return ( diff <= STANDARD_EPSILON ) && ( diff >= NEGATIVE_STANDARD_EPSILON );
        }

        public static bool EpsilonIsZero( this float a, float epsilon )
        {
            return ( a <= epsilon ) && ( a >= -epsilon );
        }

        public static bool EpsilonIsZero( this float a )
        {
            return ( a <= STANDARD_EPSILON ) && ( a >= NEGATIVE_STANDARD_EPSILON );
        }

        public static void InsertionSort<T>( this T[] array, IComparer<T> comparer )
        {
            for ( int i = 1; i < array.Length; ++i ) 
            {
                T value = array[i];
                int j = i-1;

                while ( j >= 0 && comparer.Compare( array[j], value ) > 0 )
                {
                    array[j + 1] = array[j];
                    --j;
                }

                array[j+1] = value;
            }
        }

        public static void InsertionSort<T>( this T[] array ) where T : IComparable<T>
        {
            for ( int i = 1; i < array.Length; ++i )
            {
                T value = array[i];
                int j = i - 1;

                while ( j >= 0 && array[j].CompareTo( value ) > 0 )
                {
                    array[j + 1] = array[j];
                    --j;
                }

                array[j + 1] = value;
            }
        }

        public static void InsertionSort<T>( this LinkedList<T> list ) where T : IComparable<T>
        {
            if ( list.First == null || list.First.Next == null )
                return;

            LinkedListNode<T> node = list.First.Next;

            while ( node != null )
            {
                LinkedListNode<T> currentNode = node;
                node = currentNode.Next;

                LinkedListNode<T> walkNode = currentNode.Previous;

                while ( walkNode != null && 
                    currentNode.Value.CompareTo( walkNode.Value ) < 0 )
                {
                    walkNode = walkNode.Previous;
                }

                if ( walkNode == null )
                {
                    list.Remove( currentNode );
                    list.AddFirst( currentNode );
                }
                else
                {
                    list.Remove( currentNode );
                    list.AddAfter( walkNode, currentNode );
                }
            }
        }

        public static void FindMinMax<T>( this IEnumerable<T> values, out T min, out T max )
            where T : IComparable<T>
        {
            min = values.First();
            max = min;

            foreach( T value in values )
            {
                if ( value.CompareTo( min ) < 0 )
                {
                    min = value;
                }
                else if ( value.CompareTo( max ) > 0 )
                {
                    max = value;
                }
            }
        }

        public static float RadToDeg( float rad )
        {
            return rad * ( 180.0f / FLOAT_PI );
        }

        public static float DegToRad( float degrees )
        { 
            return degrees * ( FLOAT_PI / 180.0f );
        }

        public static double RadToDeg( double rad )
        {
            return rad * ( 180.0 / Math.PI );
        }

        public static double DegToRad( double degrees )
        {
            return degrees * ( Math.PI / 180.0 );
        }
    }
}
